package com.xinsoft.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.MesWorkDayParam;
import com.xinsoft.entity.po.MesDateDevice;
import com.xinsoft.entity.po.MesWorkDate;
import com.xinsoft.service.MesDateDeviceService;
import com.xinsoft.service.MesWorkDateService;
import com.xinsoft.mapper.MesWorkDateMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lijie
 * @description 针对表【mes_work_date(工作日历)】的数据库操作Service实现
 * @createDate 2023-08-28 10:50:26
 */
@Service
public class MesWorkDateServiceImpl extends ServiceImpl<MesWorkDateMapper, MesWorkDate>
        implements MesWorkDateService {

    @Resource
    MesWorkDateMapper mesWorkDateMapper;

    @Resource
    MesDateDeviceService dateDeviceService;

    /**
     * 获取日期当月的天数
     *
     * @param dateStr yyyy-MM 或者yyyy-MM-dd
     */
    public static int getDaysByDate(String dateStr) {
        int year = Integer.parseInt(dateStr.substring(0, 4));
        int month = Integer.parseInt(dateStr.substring(5, 7));
        Calendar c = Calendar.getInstance();
        c.set(year, month, 0);
        return c.get(Calendar.DAY_OF_MONTH);
    }


    public static void main(String[] args) {
        int daysByDate = getDaysByDate("2023-08");
        System.out.println("2023年8月总天数:" + daysByDate);
    }

    @Override
    public Object getMonthData(MesWorkDayParam param) {
        // 默认本年度
        int year = DateUtil.thisYear();
        // 取某月或该月数据
        if (param.getMonth() == null) {
            param.setMonth(DateUtil.thisMonth());
        }
        String arg;
        if (param.getMonth() < 10) {
            arg = year + "-0" + param.getMonth();
        } else {
            arg = year + "-" + param.getMonth();
        }
        // 查看该年月所有数据
        List<MesWorkDate> list = mesWorkDateMapper.selectList(new QueryWrapper<MesWorkDate>().like("shift_date", arg));
        Map<String, List<MesWorkDate>> resultMap = new LinkedHashMap<>();
        if (list.size() > 0) {
            for (int i = 0; i < list.size() ; i++) {
                list.get(i).setConvertDay(DateUtil.formatDate(list.get(i).getShiftDate()));
            }
            resultMap= list.stream()
                    .collect(Collectors.groupingBy(MesWorkDate::getConvertDay));
        }
        return resultMap;
    }

    @Override
    public Object getOverDayData(MesWorkDayParam param) {
        // 默认本年度
        int year = DateUtil.thisYear();
        // 取某月或该月数据
        if (param.getMonth() == null) {
            param.setMonth(DateUtil.thisMonth());
        }
        String arg;
        if (param.getMonth() < 10) {
            arg = year + "-0" + param.getMonth();
        } else {
            arg = year + "-" + param.getMonth();
        }
        // 查看该年月所有数据
        List<MesWorkDate> list = mesWorkDateMapper.selectList(new QueryWrapper<MesWorkDate>().like("shift_date", arg)
        .eq("do_over_time", Const.Y).orderByDesc("shift_date"));
        return list;
    }


}




